package com.demo.springbooot_demo.utils;

import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


public class CustomBCryptPasswordEncoder extends BCryptPasswordEncoder {

    private int customStrength;
    private String customAlgorithm;

    public CustomBCryptPasswordEncoder(int strength, String algorithm) {
        super(strength);
        this.customStrength =   strength;
        this.customAlgorithm =  algorithm;
    }

    @Override
    public String encode(CharSequence rawPassword) {
        String salt = BCrypt.gensalt(customAlgorithm, customStrength);
        return BCrypt.hashpw(rawPassword.toString(), BCrypt.gensalt(customAlgorithm,customStrength));
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
    }

    public static void main(String[] args) {
        CustomBCryptPasswordEncoder customEncoder = new CustomBCryptPasswordEncoder(13, "$2y$");
        String rawPassword = "password123";
        String encodedPassword = customEncoder.encode(rawPassword);
        System.out.println("Encoded Password: " + encodedPassword);
        boolean result = customEncoder.matches(rawPassword,encodedPassword);
        System.out.println(result);

    }
}
